package tools;

import java.io.UnsupportedEncodingException;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendEmail {
	private static Session settings(){
		Properties props = new Properties();
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.socketFactory.port", "465");
		props.put("mail.smtp.socketFactory.class",
				"javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.port", "465");
 
		Session session =Session.getInstance(props,
			new javax.mail.Authenticator() {
				protected PasswordAuthentication getPasswordAuthentication() {
					return new PasswordAuthentication("engineerproject2013@gmail.com","inzynieria2013");
				}
			});
		return session;
	}
	
	public static void sendPasswordEmail(String email, String password) throws UnsupportedEncodingException{
		
		Session session = settings();
		try {
			Message message = new MimeMessage(session);
			InternetAddress address=new InternetAddress("test@service.com", "Internetowy Serwis Serwisu Komputerowego");
			message.setFrom(address);
			message.setRecipients(Message.RecipientType.TO,
					InternetAddress.parse(email));
			message.setSubject("Przypomnienie hasla");
			message.setText("Witaj! \n\nOto Twoje nowe haslo do zarzadzania automatami: "+password+"\nHaslo mozna zmienic w panelu uzytkownika. \n\nPozdrawiamy, \nAdministracja");			
			Transport.send(message);

 
		} catch (MessagingException e) {
			throw new RuntimeException(e);
		}
	}
	
	public static void sendDeleteEmail(String email, String name) throws UnsupportedEncodingException{
		Session session = settings();
		try {
			Message message = new MimeMessage(session);
			InternetAddress address=new InternetAddress("test@service.com", "Internetowy Serwis Serwisu Komputerowego");
			message.setFrom(address);
			message.setRecipients(Message.RecipientType.TO,
					InternetAddress.parse(email));
			message.setSubject("Usuniecie konta!");
			message.setText("Witaj "+ name+"!\n\nAdministrator usunal Twoje konto. Prawdopodobnie" +
					"nie uzywales go lub naruszyles regulamin.\n" +
					"Aby ponownie moc korzystac z serwisu - zarejestruj sie. \n\nPozdrawiamy, \nAdministracja");			
			Transport.send(message);

 
		} catch (MessagingException e) {
			throw new RuntimeException(e);
		}
	}
	
	public static void sendMessageToAdminEmail(String topic, String mes, String email, String from) throws UnsupportedEncodingException{
		Session session = settings();
		try {
			Message message = new MimeMessage(session);
			InternetAddress address=new InternetAddress(email, "Wiadomosc od uzytkownika " + from);
			message.setFrom(address);
			message.setRecipients(Message.RecipientType.TO,
					InternetAddress.parse("wojtas927@o2.pl"));
			message.setSubject(topic);
			message.setText(mes);			
			Transport.send(message);

 
		} catch (MessagingException e) {
			throw new RuntimeException(e);
		}
	}
}
